// Terrain.h

#pragma once

#include <d3d9.h>
#include <d3dx9math.h>
#include <d3dx9tex.h>
#include <d3dx9mesh.h>

#ifndef PI
#define PI 3.14159265f
#endif

struct GRIDVERTEX
{
	D3DXVECTOR3 p;  // The untransformed, 3D position for the vertex
    D3DXVECTOR3 n;  // normalized normal
	DWORD color;    // color for this vertex
};

#define D3DFVF_GRIDVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE)


class Terrain {
   private:
      int                        maxWidthVertices;    // width in vertices
      int                        maxHeightVertices;   // height in vertices
      int                        widthVertices;    // width in vertices
      int                        heightVertices;   // height in vertices
      int                        widthFaces;       // widht in faces
      int                        heightFaces;      // height in faces
      unsigned long              maxNumVertices;
      unsigned long              numVertices;
      int                        numFaces;
      D3DXVECTOR3                v0, v1, v2, v3;   // in D3D face order
      D3DXVECTOR3                dv;               // delta v from 0 to 2
      GRIDVERTEX                 *vertices;
      WORD                       *indexData;
      LPD3DXMESH				  m_Mesh;  

   public:
      Terrain(
         int                  maxWidthVerticesP,
         int                  maxHeightVerticesP,
         D3DXVECTOR3            *v0P,
         D3DXVECTOR3            *v1P,
         D3DXVECTOR3            *v2P,
         D3DXVECTOR3            *v3P
      );


      ~Terrain() {
         delete vertices;
         delete indexData;
      };


      void Update(
      );


      void GetTerrainPoint(
         D3DXVECTOR3   *pt,
         float    x,
         float    z
      );

      void GetTerrainPoint(
         D3DXVECTOR3   *pt,
         int         i,          // row (from bottom to top)
         int         j           // column (from left to right)
      );

      void GetTerrainNormal(
         D3DXVECTOR3   *n,
         D3DXVECTOR3   *pt
      );

      void GetTerrainNormal(
         D3DXVECTOR3   *n,
         int         i,          // row (from bottom to top)
         int         j           // column (from left to right)
      );


      void CreateDirectXMesh(
         LPDIRECT3DDEVICE9       pd3dDevice
      );

      void DrawDirectXMesh(
         LPDIRECT3DDEVICE9       pd3dDevice
      );


};


